home *** CD-ROM | disk | FTP | other *** search
/ Creative Computers / Creative Computers CD-ROM, Volume 1 (Legendary Design Technologies, Inc.)(1994).iso / shareware / intuition / yak_1.57 / source / main.c < prev    next >
C/C++ Source or Header  |  1994-11-17  |  10KB  |  454 lines

  1. /*
  2.  * Yak version 1.56
  3.  * ----------------
  4.  * [Yak == Yet Another K(?)ommodity
  5.  *
  6.  * There seems to be a profusion of commodities doing this or that.
  7.  * Heres mine, to do what I want it to:
  8.  *
  9.  *    AutoActivate windows (SunMouse)
  10.  *    ClickToFront, ClickToBack, ScreenCycle
  11.  *    Close/Zip/Shrink/Zoom/Turn a window via keyboard.
  12.  *    Bring up a palette on front screen.
  13.  *    Insert date into read-stream.
  14.  *    Produce key-click (like my keyclick program).
  15.  *    Some other things...
  16.  *
  17.  * Martin W. Scott & Gaël Marziou, 8/93.
  18.  */
  19. #include <exec/types.h>
  20. #include <exec/libraries.h>
  21. #include <exec/memory.h>
  22. #include <devices/inputevent.h>
  23. #include <dos/dos.h>
  24. #include <dos/dostags.h>
  25. #include <graphics/displayinfo.h>
  26. #include <libraries/commodities.h>
  27. #include <libraries/reqtools.h>
  28. #include <libraries/locale.h>
  29. #include <intuition/intuitionbase.h>
  30. #include <proto/exec.h>
  31. #include <proto/dos.h>
  32. #include <proto/graphics.h>
  33. #include <proto/commodities.h>
  34. #include <proto/intuition.h>
  35. #include <proto/reqtools.h>
  36. #include <proto/locale.h>
  37. #include <string.h>
  38. #include <stdarg.h>
  39.  
  40. #include "yak.h"
  41. #include "hotkey_types.h"
  42. #include "beep.h"
  43. #include "icon.h"
  44. #include "version.h"
  45.  
  46. #define CATCOMP_BLOCK
  47. #define CATCOMP_NUMBERS
  48. #include "yak_locale_strings.h"
  49. #undef CATCOMP_BLOCK
  50.  
  51. #include "WB2CLI.h"    /* we'll be a shell process */
  52. #define DEF_CURRENTDIR    "SYS:"
  53.  
  54.  
  55.  
  56. /* local prototypes for main.c */
  57. static void CloseResources(void);
  58. static BOOL OpenResources(void);
  59. static void FreePatterns(void);
  60. static LONG ProcessMsg(void);
  61. void __main(void);
  62.  
  63. #ifdef __SASC_60    /* save some bytes */
  64. void _MemCleanup(void){}
  65. #else
  66. void MemCleanup(void){}
  67. #endif
  68.  
  69. /* global data - library bases and the like */
  70. extern struct WBStartup *_WBenchMsg;
  71. struct Library    *CxBase, *IconBase,
  72.         *GadToolsBase, *LayersBase,
  73.         *WorkbenchBase, *LocaleBase;
  74. struct IntuitionBase *IntuitionBase;
  75. struct GfxBase *GfxBase;
  76. struct Locale *locale;
  77. struct Catalog *Catalog;
  78. struct MsgPort *broker_mp;
  79. CxObj *broker;
  80. char *PopKeyStr;
  81. #define POPKEY_EVENT    1L    /* cannot clash with YHK event... */
  82.  
  83. static const char *versionstr=VERSION_STR;
  84.  
  85. struct NewBroker newbroker = {
  86.     NB_VERSION,
  87.     "Yak",                        /* string to identify this broker */
  88.     VERSION_BROKER,
  89.     "Multi-purpose commodity",
  90.     NBU_UNIQUE | NBU_NOTIFY,    /* Don't want any new commodities
  91.                                  * starting with this name.  If someone
  92.                                  * tries it, let me know */
  93.     COF_SHOW_HIDE
  94.     };
  95.  
  96. ULONG        wndsigflag;        /* here for overlay purposes */
  97. ULONG        cxsigflag;
  98. ULONG        invariantsigflag;
  99. BYTE        palette_count;        /* how many palettes are open */
  100.  
  101. /* from handler.c */
  102. extern ULONG    clicksigflag, intuiopsigflag;
  103. extern void (*intui_routine)(APTR);    /* for intui_op's */
  104. extern APTR intui_parameter;
  105.  
  106. /* from icon.c */
  107. extern ULONG    appsigflag;
  108.  
  109. /* close what we opened */
  110. static void
  111. CloseResources()
  112. {
  113.     if (IntuitionBase) CloseLibrary((struct Library *)IntuitionBase);
  114.     if (GfxBase) CloseLibrary((struct Library *)GfxBase);
  115.     if (CxBase) CloseLibrary(CxBase);
  116.     if (LayersBase) CloseLibrary(LayersBase);
  117.     if (IconBase) CloseLibrary(IconBase);
  118.     if (GadToolsBase) CloseLibrary(GadToolsBase);
  119.     if (WorkbenchBase) CloseLibrary(WorkbenchBase);
  120.     if (LocaleBase)
  121.     {
  122.         CloseCatalog (Catalog); /* NULL is valid */
  123.         CloseLocale(locale);
  124.         CloseLibrary(LocaleBase);
  125.     }
  126. }
  127.  
  128. /* open libraries, devices that we need */
  129. static BOOL
  130. OpenResources(void)
  131. {
  132.     if ((IntuitionBase = (void *)OpenLibrary("intuition.library", 37L)) &&
  133.         (GfxBase = (void *)OpenLibrary("graphics.library", 37L)) &&
  134.         (CxBase = OpenLibrary("commodities.library", 37L)) &&
  135.         (LayersBase = OpenLibrary("layers.library", 37L)) &&
  136.         (IconBase = OpenLibrary("icon.library", 37L)) &&
  137.         (GadToolsBase = OpenLibrary("gadtools.library", 37L)) &&
  138.         (WorkbenchBase = OpenLibrary("workbench.library", 37L)) &&
  139.         (DOSBase = (struct DosLibrary *)OpenLibrary("dos.library", 37L)))
  140.     {
  141.         return TRUE;
  142.     }
  143.     CloseResources();
  144.     return FALSE;
  145. }
  146.  
  147.  
  148. /* open locale library and catalog */
  149. void
  150. OpenLocaleStuff(char *language)
  151. {
  152.  
  153.     if (LocaleBase =(struct LocaleBase *)OpenLibrary("locale.library", 38L))
  154.     {
  155.         if (!(locale = OpenLocale(NULL)))
  156.         {    
  157.             PostError("No locale set!");
  158.             CloseLibrary(LocaleBase);
  159.             LocaleBase = NULL;
  160.         }
  161.         Catalog = OpenCatalog(     locale, "yak.catalog", 
  162.                               OC_BuiltInLanguage, "english", 
  163.                               OC_Language, language,        
  164.                               OC_Version, 15L, 
  165.                               TAG_DONE );
  166.     }    
  167. }
  168.  
  169.  
  170. /* slighlty modified version of GetString() generated by catcomp */
  171.  
  172. char *getString(ULONG MsgID)
  173. {
  174. LONG   *l;
  175. UWORD  *w;
  176. char *builtIn;
  177.  
  178.    l = (LONG *)CatCompBlock;
  179.  
  180.    while (*l != MsgID )
  181.    {
  182.        w = (UWORD *)((ULONG)l + 4);
  183.        l = (LONG *)((ULONG)l + (ULONG)*w + 6);
  184.    }
  185.    builtIn = (char *)((ULONG)l + 6);
  186.    if (LocaleBase)
  187.        return (GetCatalogStr (Catalog, MsgID, builtIn));
  188.    return(builtIn);
  189. }
  190.  
  191. /* simple requester with args */
  192. void
  193. PostError(char *body, ... )
  194. {
  195.     struct EasyStruct es;
  196.     va_list args;
  197.  
  198.     if (!IntuitionBase)
  199.     {
  200.         Write(Output(), "Need AmigaDos 2.0+\n", -1);
  201.         return;
  202.     }
  203.  
  204.     /* setup the argument array */
  205.     va_start( args, body );
  206.  
  207.     /* initialise the structure */
  208.     es.es_StructSize = sizeof(struct EasyStruct);
  209.     es.es_Flags = 0L;
  210.     es.es_Title = getString(Error_Requester_Title);
  211.     es.es_TextFormat = body;
  212.     es.es_GadgetFormat = "OK";
  213.  
  214.     /* display the requester */
  215.     EasyRequestArgs(NULL, &es, NULL, args);
  216.  
  217.     /* free the arguments */
  218.     va_end( args );
  219. }
  220.  
  221. /* parse pattern, report errors */
  222. BOOL __regargs
  223. InitPattern(char *newpatstr, UWORD n)
  224. {
  225.     char *patstr = newpatstr ? newpatstr : patterns[n].patstr;
  226.     char *pat;
  227.     LONG len;
  228.  
  229.     if (pat = AllocVec(len = strlen(patstr)*3+10, MEMF_CLEAR))
  230.     {
  231.         if (ParsePattern(patstr, pat, len) != -1)
  232.         {
  233.             if (newpatstr) strncpy(patterns[n].patstr, newpatstr, PATLEN);
  234.             if (patterns[n].pat) FreeVec(patterns[n].pat);
  235.             patterns[n].pat = pat;
  236.             return TRUE;
  237.         }
  238.         
  239.         PostError("%s:\n\"%s\"", getString(Parsing_Pattern_ERR), patstr);
  240.         FreeVec(pat);
  241.     }
  242.     else PostError(getString(Allocation_ERR));
  243.     return FALSE;
  244. }
  245.  
  246. static void
  247. FreePatterns()
  248. {
  249.     UWORD i;
  250.  
  251.     for (i = 0; i < NUM_PATTERNS; i++)
  252.         if (patterns[i].pat) FreeVec(patterns[i].pat);
  253. }
  254.  
  255. void __main()        /* Yak: multi-function commodity */
  256. {
  257.     BPTR    newdir = NULL, olddir;
  258.  
  259.     if (OpenResources())
  260.     {
  261.         if (broker_mp = CreateMsgPort())
  262.         {
  263.             newbroker.nb_Port = broker_mp;
  264.             cxsigflag = 1L << broker_mp->mp_SigBit;
  265.  
  266.             /* process tool-types */
  267.             GetOurIcon(_WBenchMsg);
  268.             newbroker.nb_Pri = (BYTE)TTInt("CX_PRIORITY", 0);
  269.  
  270.             if (_WBenchMsg)
  271.             {
  272.                 if (newdir = Lock(DEF_CURRENTDIR, ACCESS_READ))
  273.                     olddir = CurrentDir(newdir);
  274.                 WB2CLI(_WBenchMsg,4000,DOSBase);        /* get it over with... */
  275.             }
  276.  
  277.             if (broker = CxBroker(&newbroker, NULL))
  278.             {
  279.                 /* HANDLER FIRST, SO IT SEES EVERYTHING!!! */
  280.                 if (InitHandler())
  281.                 {    
  282.                     InitYakHotKeyList();
  283.                     LoadSettings();
  284.                     
  285.                     /* Open the right locale if tooltype LANGUAGE is used */
  286.                     OpenLocaleStuff(TTString("LANGUAGE",NULL));
  287.  
  288.                     if (PopKeyStr = DupStr(TTString("CX_POPKEY", "Rcommand help")))
  289.                     {
  290.                         CxObj *tmpobj;
  291.                     
  292.                         if (tmpobj = HotKey(PopKeyStr, broker_mp, POPKEY_EVENT))
  293.                             AttachCxObj(broker, tmpobj);
  294.                         else
  295.                             PostError("%s:\"%s\"", getString(CX_POPKEY_invalid_ERR), 
  296.                                       PopKeyStr);
  297.                     }
  298.                     /* else... if this failed, we lose */
  299.  
  300.                     MyPri(ACTIVE);
  301.                     ActivateCxObj(broker, 1L);
  302.  
  303.                     if (TTBool("CX_POPUP", FALSE))
  304.                         ShowWindow();
  305.  
  306.                     if (TTBool("APPICON", FALSE))
  307.                     {
  308.                         if (!MakeOurAppIcon(TTString("ICONNAME", "Yak!")))
  309.                             if (_WBenchMsg)
  310.                                 PostError(getString(Couldn_t_create_AppIcon_ERR));
  311.                     }
  312.                     /* WB makes a copy of icon, so can free it */
  313.                     FreeOurIcon();
  314.  
  315.                     /* these are the signals waited for, + window sig */
  316.                     invariantsigflag = SIGBREAKF_CTRL_C | cxsigflag
  317.                         | clicksigflag | intuiopsigflag
  318.                             | appsigflag;
  319.  
  320.                     while (ProcessMsg())
  321.                         ;
  322.                     HideWindow();
  323.                     RemoveOurAppIcon();
  324.                     DeleteYakHotKeyList();
  325.  
  326.                     FreeStr(PopKeyStr);
  327.                     MyPri(ORIGINAL);
  328.  
  329.                     EndHandler();
  330.                     FreePatterns();
  331.                 }
  332.                 else 
  333.                     PostError(getString(Allocation_ERR));
  334.     
  335.                 DeleteCxObjAll(broker);
  336.             }
  337.  
  338.             if (newdir) 
  339.             {
  340.                 CurrentDir(olddir);
  341.                 UnLock(newdir);
  342.             }
  343.             DeleteMsgPort(broker_mp);
  344.